<template>
  <div class="authentication">
    <div class="com-top-nav">
      <van-nav-bar
        title="添加or编辑工作经历"
        left-arrow
        @click-left="onClickLeft"
      />
    </div>
    <div>
      <van-cell-group>
        <p class="cell-group-tips">我的工作经历（必填）</p>
        <van-field
          v-model="formData.companyName"
          required
          clearable
          label="公司"
          placeholder="请填写公司名称"
        />
        <!--{{formData.timeRange}}-->
        <zx-timerange-picker :required="true" v-model="formData.timeRange" title="工作时间"></zx-timerange-picker>
        <van-field
          v-model="formData.position"
          required
          clearable
          label="职位"
          placeholder="请填写职位"
        />
        <zx-area-picker :required="true" v-model="formData.city" title="工作城市"></zx-area-picker>
        <p class="cell-group-tips">工作内容和职责</p>
        <van-field
          type="textarea"
          v-model="formData.reward"
          clearable
          :autosize="{maxHeight: '300', minHeight: '100'}"
          placeholder="请简单说明您的工作内容，关键成绩等。填写会更容易被录取哦。"
        />
      </van-cell-group>
      <div class="button">
        <van-button size="large" @click="handleSubmit">保存</van-button>
      </div>

    </div>
  </div>
</template>
<script>
import validator from "../../../common/validatorFunction";
import zxPicker from "../../../components/zx-picker";
import zxAreaPicker from "../../../components/zx-area-picker";
import zxTimerangePicker from "../../../components/zx-timerange-picker";

export default {
  components: {
    zxPicker,
    zxAreaPicker,
    zxTimerangePicker
  },
  created(){
    this.init()
  },
  data() {
    return {
      isShowError: false,
      errorTips: "",
      majorList: [{ values: ["1", "2"] }],
      educationList: [{ values: ["1", "3"] }],
      formData: {
        companyName: "",
        position: "",
        reward: "",
        timeRange: ["2013-08-12", "2015-08-12"],
        city: [
          { code: "140000", name: "山西省" },
          { code: "140400", name: "长治市" }
        ]
      },
      rules: {
        companyName: [{ require: true, message: "请输入公司名称" }],
        position: [{ require: true, message: "请输入职位" }],
        timeRange: [{ require: true, message: "请选择时间" }],
        city: [{ require: true, message: "请选择工作地点" }]
      }
    };
  },
  mounted() {},
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    handleSubmit() {
      // 保存数据
      const rulesResult = validator(this.formData, this.rules);
      console.log(rulesResult);
      if (rulesResult.result) {
        let workId = this.$route.params.workId;
        let uid = JSON.parse(sessionStorage.getItem("user")).uid;
        let company = this.formData.companyName;
        let start_time = this.formData.timeRange[0];
        let end_time = this.formData.timeRange[1];
        let address = this.formData.companyName;
        let position = this.formData.position;
        let des = this.formData.reward;
        let params = {
          workId,
          uid,
          company,
          start_time,
          end_time,
          address,
          position,
          des
        };
        this.axios
          .get("/api/user/job/create", { params })
          .then(res => {
            if (res.data.err_code == 0) {
              if(workId >= 0){
                this.$toast({message:"修改成功！", position: 'middle'});  
              }
              else{
                this.$toast({message:"添加成功！", position: 'middle'});
              }              
            }
          })
          .catch(e => {
            this.$toast({ message: e, position: "middle" });
          });
        this.$router.go(-1);
        // }
      } else {
        const alertMessage = [];
        for (const key in rulesResult.detail) {
          if (!rulesResult.detail[key].isPass) {
            alertMessage.push(rulesResult.detail[key].message);
          }
        }
        this.isShowError = true;
        this.errorTips = alertMessage[0];
        this.$toast({ message: this.errorTips, position: "middle" });
      }
    },
    init(){
        let workId = this.$route.params.workId;
        if(workId >= 0){
          this.axios.get('/api/user/job/showById',{params:{workId}}).then(res=>{
            console.log(res.data.data);
              if(res.data.err_code === 0){
                this.formData.companyName = res.data.data.company;
                this.formData.timeRange = [res.data.data.start_time, res.data.data.end_time];
                this.formData.position = res.data.data.position;
                this.formData.reward = res.data.data.des;
              }
          }).catch(e=>{
            this.$toast({message:e})
          })          
        }
        else{
          //this.formData.major[0] = '计算机';
          //this.formData.education[0] = '本科';
        }      
    }
  }
};
</script>

<style scoped lang="less" type="text/less">
@import url("../../../../static/style/var");
.cell-group-tips {
  margin: 0;
  font-weight: 400;
  font-size: 14px;
  color: @weak-tips-color;
  padding: 10px 15px;
}
.button {
  padding-left: 10px;
  padding-right: 10px;
  margin-top: 30px;
}
</style>
